package com.community.sqlapp.service;

import com.community.sqlapp.dpsToXmind.QwenHttpClientUtil;
import com.community.sqlapp.entity.FileInfoPo;
import com.community.sqlapp.mapper.FileInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 千问大模型，生成测试点实现类
 */
@Service
public class QwenVlToXmindService {

    @Autowired
    private QwenHttpClientUtil httpClientUtil;

    @Autowired
    private FileInfoMapper fileInfoMapper;

    /**
     * 图片请求qwen-vl-plus大模型
     * @param imageUrl
     * @param userPrompt
     * @return
     * @throws IOException
     */
    public String parseDocument(String imageUrl, String userPrompt) throws IOException {
        String dashscopeModel = "qwen-vl-plus"; // 可从配置读取
        // 智能prompt，要求AI只返回JSON结构
        String smartPrompt = (userPrompt == null || userPrompt.isEmpty() ? "请根据上传的需求文档，自动提取所有功能点并生成详细的测试点，要求结构化、分层级、覆盖全面。" : userPrompt);
        System.out.println("userPrompt: "+userPrompt);
        StringBuilder jsonBody = new StringBuilder();
        jsonBody.append("{");
        jsonBody.append("\"model\": \"").append(dashscopeModel).append("\",");
        jsonBody.append("\"messages\": [");
        jsonBody.append("  {");
        jsonBody.append("    \"role\": \"user\",");
        jsonBody.append("    \"content\": [");
        // 图片内容
        jsonBody.append("      {\"type\": \"image_url\", \"image_url\": {\"url\": \"").append(imageUrl).append("\"}},");
        // 文本内容
        jsonBody.append("      {\"type\": \"text\", \"text\": \"").append(smartPrompt).append("\"}");
        jsonBody.append("    ]");
        jsonBody.append("  }");
        jsonBody.append("]");
        jsonBody.append("}");
        System.out.println("请求体： "+jsonBody.toString());
        return httpClientUtil.sendPostRequest(jsonBody.toString());
    }

    /**
     * word文档使用qwen-long大模型
     * @param fileName 文件名
     * @param userPrompt 用户提示
     * @return
     * @throws IOException
     */
    public String parseLongDocument(String fileName, String userPrompt) throws IOException {
        String dashscopeModel = "qwen-long"; // 可从配置读取
        // 智能prompt，要求AI只返回JSON结构
        String smartPrompt = (userPrompt == null || userPrompt.isEmpty() ? "请根据上传的需求文档，自动提取所有功能点并生成详细的测试点，要求结构化、分层级、覆盖全面。" : userPrompt);

        // 从数据库获取fileid
        FileInfoPo fileInfo = fileInfoMapper.getLatestFileInfoByFileName(fileName);
        if (fileInfo == null) {
            throw new IOException("未找到文件信息：" + fileName);
        }

        String fileId = fileInfo.getFileId();
        System.out.println("从数据库获取的fileId: "+fileId);

        StringBuilder jsonBody = new StringBuilder();
        jsonBody.append("{" +
                "    \"model\": \""+dashscopeModel+"\"," +
                "    \"input\": {" +
                "        \"messages\": [" +
                "            {\"role\": \"system\",\"content\": \"fileid://"+fileId+"\"}," +
                "            {\"role\": \"user\",\"content\": \""+smartPrompt+"\"}" +
                "        ]" +
                "    }," +
                "    \"parameters\": {" +
                "        \"result_format\": \"message\"" +
                "    }" +
                "}");
        System.out.println("请求体： "+jsonBody.toString());
        return httpClientUtil.sendGetLongRequest(jsonBody.toString());
    }
}